﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.IO;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NCommon;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Paso01")]
    public class Paso01Controller : ApiController
    {   

        [HttpPost]
        [Route("PostPaso01_inq")]

        //多记录查询
        public System.Data.DataSet PostPaso01_inq([FromBody]  JObject obj)
        {
 
            

            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            EI.EIInfo inBlock = new EI.EIInfo();

            inBlock.Tables[0].Columns.Add("FLAG");
            inBlock.Tables[0].Columns.Add("STOCK_NO");
            inBlock.Tables[0].Columns.Add("APPLY_BILL_NO");
            inBlock.Tables[0].Columns.Add("REC_CREATE_TIME");
            inBlock.Tables[0].Columns.Add("REC_REVISE_TIME");
            inBlock.Tables[0].Columns.Add("REC_CREATOR");
           
           

            inBlock.Tables[0].Rows.Add();

            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.userid = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            inBlock.sys_info.username = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            inBlock.sys_info.formname = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();

            inBlock.Tables[0].Rows[0]["FLAG"] = "PASO01";
            inBlock.Tables[0].Rows[0]["STOCK_NO"] = obj["STOCK_NO"] == null ? " " : obj["STOCK_NO"].ToString().Trim();
            inBlock.Tables[0].Rows[0]["APPLY_BILL_NO"] = obj["APPLY_BILL_NO"] == null ? " " : obj["APPLY_BILL_NO"].ToString();
            inBlock.Tables[0].Rows[0]["REC_CREATOR"]        = obj["REC_CREATOR"] == null ? " " : obj["REC_CREATOR"].ToString();
            inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"]    = obj["REC_CREATE_TIME"] == null ? " " : obj["REC_CREATE_TIME"].ToString();
            inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"]    = obj["REC_REVISE_TIME"] == null ? " " : obj["REC_REVISE_TIME"].ToString();   

           

            //仅查询最近最近2天的；不然数据太多，影响小程序效率；
            if (inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"].ToString().Trim() == "")
            {
                inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] = DateTime.Now.AddMonths(-1).ToString("yyyyMMddHHmmss");
            }

            if (inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"].ToString().Trim() == "")
            {
                inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"] = DateTime.Now.AddDays(1).ToString("yyyyMMddHHmmss");
            }
            
            EI.EIInfo outBlock = EI.EITuxedo.CallService("paso05_inq", inBlock);
            


            //小代码转换；
            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("FIDP","PA37");
            DataTable dtTemp = null;

            EI.EIInfo outBlock_TEMP2 = EF.Utility.ExecQuery("select t.STOCK_NO CODE ,t.stock_name as CODE_DESC_1_CONTENT from tsimm01 t");

           //EI.EIInfo outBlock_TEMP3 = EF.Utility.ExecQuery("select companycode CODE ,companyname CODE_DESC_1_CONTENT from es.tescompanyinfo ");
             

            for (int i = outBlock.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                string STOCK_NO = outBlock.Tables[0].Rows[i]["STOCK_NO"].ToString();
                if (STOCK_NO.Trim() != "")
                {
                    dtTemp = outBlock_TEMP2.Tables[0].Select("CODE  = '" + STOCK_NO + "'").CopyToDataTable<DataRow>();
                    outBlock.Tables[0].Rows[i]["STOCK_NO"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }


                string DPTMENT_CODE = outBlock.Tables[0].Rows[i]["DPTMENT_CODE"].ToString();
                if (DPTMENT_CODE.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["FIDP"].Select("CODE  = '" + DPTMENT_CODE + "'").CopyToDataTable<DataRow>();
                    outBlock.Tables[0].Rows[i]["DPTMENT_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }

                string ASMAT_USE_TYPE = outBlock.Tables[0].Rows[i]["ASMAT_USE_TYPE"].ToString();
                if (ASMAT_USE_TYPE.Trim() != "")
                {
                    dtTemp = outBlock_TEMP.Tables["PA37"].Select("CODE  = '" + ASMAT_USE_TYPE + "'").CopyToDataTable<DataRow>();
                    outBlock.Tables[0].Rows[i]["ASMAT_USE_TYPE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                }


                string PA_RECEIPT_DATE = outBlock.Tables[0].Rows[i]["AUDIT_DATE"].ToString();
                if (PA_RECEIPT_DATE.Length >= 8)
                    outBlock.Tables[0].Rows[i]["AUDIT_DATE"] = PA_RECEIPT_DATE.Substring(0, 4) + "-" + PA_RECEIPT_DATE.Substring(4, 2) + "-" + PA_RECEIPT_DATE.Substring(6, 2);



                string REC_CREATE_TIME = outBlock.Tables[0].Rows[i]["REC_CREATE_TIME"].ToString();
                if (REC_CREATE_TIME.Length >= 8)
                    outBlock.Tables[0].Rows[i]["REC_CREATE_TIME"] = REC_CREATE_TIME.Substring(0, 4) + "-" + REC_CREATE_TIME.Substring(4, 2) + "-" + REC_CREATE_TIME.Substring(6, 2);
                
            }

            
            
            outBlock.Tables[0].TableName = "Table0";
            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);            
            return outBlock;


        }


        [HttpPost]
        [Route("PostPaso01_Info")]

        //单记录查询
        public System.Data.DataSet PostPaso01_Info([FromBody]  JObject obj)
        {

             EI.EIInfo inBlock = new EI.EIInfo();
           

            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
             string apply_bill_no = obj["APPLY_BILL_NO"] == null ? " " : obj["APPLY_BILL_NO"].ToString();
             
             
             string sql = "select ASMAT_NO,ASMAT_NAME ,SPEC,GET_NUM,UNIT,GET_NUM ,GET_ASSIST_NUM,ASSIST_UNIT ,OLDTONEW_FLAG,APN  from  TPASO04 ta   " +
             " where ta.apply_bill_no = '" + apply_bill_no + "' ";

             EI.EIInfo outBlock = EF.Utility.ExecQuery(sql);

           
            //EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("PA08","PA09", "FIBR", "FIDP");

           
           

            DataTable dtTemp = null;
            
            //主项代码转换；
            for (int i = outBlock.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                 
                //string BILL_NO = outBlock.Tables[1].Rows[i]["PA_RECEIPT_NO"].ToString();
                //string NAME_SUB = outBlock.Tables[1].Rows[i]["ASMAT_NO"].ToString();
                 
                //var mapPath   = "D:\\pic\\_pic_new\\paso\\" + BILL_NO.Substring(2, 3) + "\\" + BILL_NO + "\\";

                ////借用字段 NO_STOCK_QTY	未入库量(参考)，存放图片数量
                //outBlock.Tables[1].Rows[i]["NO_STOCK_QTY"] = 0;
                //if (Directory.Exists(mapPath))
                //{
                //    string[] file = Directory.GetFiles(mapPath, NAME_SUB + "*");
                //    outBlock.Tables[1].Rows[i]["NO_STOCK_QTY"] =  file.Length;
                //}

                 
               

                /*
                string branch_code = outBlock.Tables[1].Rows[i]["BRANCH_CODE"].ToString();
                if (branch_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIBR"].Select("CODE  = '" + branch_code + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[1].Rows[i]["BRANCH_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                

                string dptment_code = outBlock.Tables[1].Rows[i]["DPTMENT_CODE"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIDP"].Select("CODE  = '" + dptment_code + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[1].Rows[i]["DPTMENT_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                

                string BILL_STATUS = outBlock.Tables[1].Rows[i]["BILL_STATUS"].ToString();
                if (BILL_STATUS.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PA09"].Select("CODE  = '" + BILL_STATUS + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[1].Rows[i]["BILL_STATUS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                */


       
            }





            outBlock.Tables[0].TableName = "T0";
            //outBlock.Tables[1].TableName = "T1";


            ////[增加主项]  T0 就是 TMAIN 
            //System.Data.DataSet  outBlock_main = PostPaso01_inq(obj);
            //outBlock_main.Tables[0].TableName = "TMAIN";
            //outBlock.Tables.Add(outBlock_main.Tables[0].Copy());
             
            //仓库
           
           

            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }



     


        [HttpPost]
        [Route("PostPaso01_Load")]

        //高级查询的查询条件获取，【主要是各类小代码，类似之前画面的初始化参数】
        public System.Data.DataSet PostPaso01_Load([FromBody]  JObject obj)
        {           

            //小代码转换；
           
 
            EI.EIInfo inBlock = new EI.EIInfo();

            inBlock.sys_info.userid = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            EI.EIInfo outBlock = EI.EITuxedo.CallService("pasi01_inqck2", inBlock);
            outBlock.Tables[0].Columns[0].ColumnName = "CODE";
            DataRow dr3 = outBlock.Tables[0].NewRow();
            dr3["CODE"] = " ";
            dr3["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables[0].Rows.InsertAt(dr3, 0);
            outBlock.Tables[0].TableName = "STOCK_NO";

            //outBlock.Tables.Add(outBlock.Tables[0].Copy());




            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }


        //[HttpPost]
        //[Route("PostPaso01_pro")]
 
    }
}